from wtforms import StringField, IntegerField
from wtforms.validators import DataRequired, length, ValidationError, Email
from app.libs.enums import ClientTypeEnum
from app.models.user import User
from app.forms.base import BaseForm as Form


class ClientForm(Form):
    account = StringField(validators=[DataRequired(message='请输入账号')])
    secret = StringField(validators=[DataRequired(message='请输入密码')])
    type = IntegerField(validators=[DataRequired(message='type参数不能为空')])

    def validate_type(self, value):
        try:
            client_type = ClientTypeEnum(value.data) # 将Enum的key传入将得到一个EnumName.key的一个返回值
        except ValueError as e:
            raise ValidationError('不是有效的type类型')
        self.type.data = client_type


class UserEmailForm(ClientForm):
    account = StringField(validators=[DataRequired(message='请输入账号'), Email(message='请输入正确的邮箱格式')])
    secret = StringField(validators=[DataRequired(message='密码不能为空'), length(6, 20, message='请输6-20位的密码')])
    nickname = StringField(validators=[DataRequired(message='请输入用户名'), length(
        min=3, max=20, message='请输入3-20位的用户名')])
    
    def validate_account(self, value):
        if User.query.filter_by(email=value.data).first():
            raise ValidationError('邮箱重复')